package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.PhotoMapper;
import com.jd.finance.core.pojo.entity.Photo;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.core.service.PhotoService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-19
 */
@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Autowired
    private PhotoMapper photoMapper;

    @Autowired
    private AdminUtilsServiceImpl adminUtilsService;

    @Resource
    private OpreateLogService opreateLogService;

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;


    @Override
    public BaseResult getAdvertisePhoto() {

        QueryWrapper<Photo> qw = new QueryWrapper<>();
        qw.eq("type", 1);
        List<Photo> list = photoMapper.selectList(qw);
        if (list == null) {
            return BaseResult.error().message("未查询到广告位图片");
        }
        return BaseResult.ok().data("photoLists", list);
    }

    @Override
    public BaseResult getBanner() {
        QueryWrapper<Photo> qw = new QueryWrapper<>();
        qw.eq("status", 0)
                .select("photo_address", "photo_url");
        List<Photo> photos = photoMapper.selectList(qw);
        if (photos == null && photos.size() == 0) {
            return BaseResult.error().message("查询轮播图片为空");
        }
        return BaseResult.ok().data("divohotos:", photos);
    }

    /**
     * 查询所有banner图
     * @return
     */
    @Override
    public BaseResult getAllBannerPhoto() {

        QueryWrapper<Photo> qw = new QueryWrapper<>();
        qw.lambda().eq(Photo::getType,0);
        List<Photo> photoList = photoMapper.selectList(qw);

        return BaseResult.ok().data("List",photoList);
    }

    /**
     * 添加banner图
     * @param uid
     * @param photoPath
     * @param photoUrl
     * @param request
     * @return
     */
    @Override
    public BaseResult insertBannerPhoto(Integer uid, String photoPath, String photoUrl, HttpServletRequest request) {
        if (uid == null) {
            log.error("传入id为空");
            return BaseResult.error().message("传入id为空");
        }
        if (StringUtils.isAllBlank(photoPath, photoUrl)) {
            log.error("传入信息为空");
            return BaseResult.error().message("传入信息为空");
        }
        if (!adminUtilsService.isAdmin(uid)){
            log.error("无访问权限");
            return BaseResult.error().message("无访问权限");
        }

        Photo photo = new Photo();
        photo.setPhotoUrl(photoUrl);
        photo.setPhotoAddress(photoPath);
        photo.setType(0);
        int insert = photoMapper.insert(photo);
        //添加操作日志
        boolean add = opreateLogService.add(uid, "添加", "添加banner图（图片路径、url）", IpUtil.getIpAddr(request));
        if (!add){
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }

        if (insert >= 1){
            return BaseResult.ok().data("tianjiajieguo",insert);

        }else {
            return BaseResult.ok().data("tianjiajieguo",0);
        }
    }

    /**
     * 删除banner图
     * @param uid
     * @param bannerId
     * @param request
     * @return
     */
    @Override
    public BaseResult deleteBannerPhoto(Integer uid, Integer bannerId, HttpServletRequest request) {
        if (uid == null || bannerId == null) {
            log.error("传入id为空");
            return BaseResult.error().message("传入id为空");
        }
        if (!adminUtilsService.isInformation(uid)){
            log.error("无访问权限S");
            return BaseResult.error().message("无访问权限S");
        }
        int delete = photoMapper.deleteById(bannerId);
        //添加操作日志
        boolean add = opreateLogService.add(uid, "删除", "删除banner图", IpUtil.getIpAddr(request));
        if (!add){
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }


        if (delete >= 1){
            return BaseResult.ok().data("shanchujieguo",delete);

        }else {
            return BaseResult.ok().data("shanchujieguo",0);
        }


    }

    /**
     * 修改banner排序序号
     * @param uid
     * @param photoId
     * @param bannerNumber
     * @param request
     * @return
     */
    @Override
    public BaseResult updateBannerSeq(Integer uid, Integer photoId, Integer bannerNumber, HttpServletRequest request) {
        if (uid == null || bannerNumber == null){
            return BaseResult.error().message("参数为空");
        }
        if (!adminUtilsService.isInformation(uid)){

            log.error("无访问权限S");
            return BaseResult.error().message("无访问权限S");
        }


        QueryWrapper<Photo> qw = new QueryWrapper<>();
        qw.lambda().eq(Photo::getPhotoId,photoId);
        Photo photo1 = photoMapper.selectOne(qw);
       if ( photo1.getType() != 0  ){
           log.error("该图片不是轮播图");
           return BaseResult.error().message("该图片不是轮播图");
       }

        Photo photo = new Photo();
        photo.setPhotoId(photoId);
        photo.setNumber(bannerNumber);
        int update = photoMapper.updateById(photo);
        //添加操作日志
        boolean add = opreateLogService.add(uid, "修改", "修改banner图", IpUtil.getIpAddr(request));
        if (!add){
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }
        //返回结果集
        if (update >= 1){
            return BaseResult.ok().data("xiugaichenggong",update);

        }else {
            return BaseResult.ok().data("xiugaishibai",0);
        }

    }

    /**
     * 更新腰线图片路径
     * @param uid
     * @param photoId
     * @param url
     * @param request
     * @return
     */
    @Override
    public BaseResult updateLineSrc(Integer uid, Integer photoId, String url, HttpServletRequest request) {
        if (uid==null) {
            log.error("id为空!");
            return BaseResult.error().message("id为空");
        }

        QueryWrapper<Photo> qw = new QueryWrapper<>();
        qw.eq("photo_id",photoId);
        Photo photo1 = photoMapper.selectOne(qw);
        if (photo1.getType() != 2){
            log.error("该图片不是腰线图!");
            return BaseResult.error().message("该图片不是腰线图");
        }

        Photo photo = new Photo();
        photo.setPhotoId(photoId);
        photo.setPhotoUrl(url);

        int i = photoMapper.updateById(photo);
        if (i == 0){
            log.error("更新失败");
            return BaseResult.error().message("更新失败");
        }
        boolean add=opreateLogService.add(uid,"更新","更新腰线图片路径", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("gengxinjieguo:",1);
    }


}
